package com.boot.web.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import com.boot.web.dao.AccountRecordDao;
import com.boot.web.dto.AccountRecordDTO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.boot.communal.paging.InitPaging;
import com.boot.communal.response.ResponseServer;
import com.boot.web.entity.AccountRecord;
import com.boot.web.service.AccountRecordService;

/**
 * 账户记录控制器
 * @author 【zhouJie】
 * @date 2021-06-02 08:43:21
 */
@RestController
@RequestMapping("/accountRecord")
public class AccountRecordController {

    @Autowired
    private AccountRecordService accountRecordService;

    @Autowired
    private AccountRecordDao accountRecordDao;

    @PostMapping("/save")
    public ResponseServer save(AccountRecordDTO dto) {
        AccountRecord accountRecord = new AccountRecord();
        BeanUtils.copyProperties(dto, accountRecord);
        if(!"".equals(dto.getIntoUserId())){
            accountRecord.setIntoUserId(Integer.parseInt(dto.getIntoUserId()));
        }
        if(!"".equals(dto.getOutUserId())){
            accountRecord.setOutUserId(Integer.parseInt(dto.getOutUserId()));
        }
        if(!"".equals(dto.getType())){
            accountRecord.setType(dto.getType());
        }else{
            return ResponseServer.error("操作类型不能为空");
        }
        if("".equals(dto.getSign())){
            return ResponseServer.error("标记不能为空");
        }
        if("".equals(dto.getAmount())){
            return ResponseServer.error("操作数量不能为空");
        }else{
            accountRecord.setAmount(BigDecimal.valueOf(Double.valueOf(dto.getAmount())));
        }
        if("".equals(dto.getNowAmount())){
            return ResponseServer.error("操作后的数量不能为空");
        }else{
            accountRecord.setNowAmount(BigDecimal.valueOf(Double.valueOf(dto.getNowAmount())));
        }
        accountRecord.setUpdateTime(new Date());
        return ResponseServer.success("添加成功", accountRecordService.save(accountRecord));
    }

    @PutMapping("/update")
    public ResponseServer update(AccountRecordDTO dto) {
        AccountRecord accountRecord = new AccountRecord();
        BeanUtils.copyProperties(dto, accountRecord);
        if(!"".equals(dto.getIntoUserId())){
            accountRecord.setIntoUserId(Integer.parseInt(dto.getIntoUserId()));
        }
        if(!"".equals(dto.getOutUserId())){
            accountRecord.setOutUserId(Integer.parseInt(dto.getOutUserId()));
        }
        if(!"".equals(dto.getType())){
            accountRecord.setType(dto.getType());
        }else{
            return ResponseServer.error("操作类型不能为空");
        }
        if("".equals(dto.getSign())){
            return ResponseServer.error("标记不能为空");
        }
        if("".equals(dto.getAmount())){
            return ResponseServer.error("操作数量不能为空");
        }else{
            accountRecord.setAmount(BigDecimal.valueOf(Double.valueOf(dto.getAmount())));
        }
        if("".equals(dto.getNowAmount())){
            return ResponseServer.error("操作后的数量不能为空");
        }else{
            accountRecord.setNowAmount(BigDecimal.valueOf(Double.valueOf(dto.getNowAmount())));
        }
        accountRecord.setUpdateTime(new Date());
        return ResponseServer.success("修改成功", accountRecordService.update(accountRecord));
    }

    @DeleteMapping("/delete/{id}")
    public ResponseServer delete(@PathVariable Integer id) {
        return ResponseServer.success("删除成功", accountRecordService.delete(id));
    }

    @DeleteMapping("/batchDelete/{ids}")
    public ResponseServer batchDelete(@PathVariable Integer[] ids) {
        return ResponseServer.success("批量删除成功", accountRecordService.batchDelete(ids));
    }

    @GetMapping("/getPageList")
    public ResponseServer getPageList(@RequestParam Map<String, Object> params) {
        InitPaging.init(params);
        return ResponseServer.success(new PageInfo(accountRecordService.getAccountRecordVOPageList(params)));
    }

    @GetMapping("/getAll")
    public ResponseServer getAll() {
        return ResponseServer.success(accountRecordDao.queryAllVO());
    }

    @GetMapping("/getById/{id}")
    public ResponseServer getById(@PathVariable Integer id) {
        return ResponseServer.success(accountRecordService.getById(id));
    }

}
